package com.example.ArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class Test4Collection {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        Student s1 = new Student("张三", 23);
        Student s2 = new Student("李四", 19);
        Student s3 = new Student("王五", 22);

        list.add(s1);
        list.add(s2);
        list.add(s3);

        //迭代器遍历
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student student = it.next();
            System.out.println(student);
        }
        System.out.println("-----------------------------");
        //增强for
        for (Student student : list) {
            System.out.println(student);
        }
    }
}

class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}